/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.chigs.web.admin.configlocationgroup;

import com.googlecode.chigs.domain.ConfigLocation;
import com.googlecode.chigs.domain.ConfigLocationGroup;
import com.googlecode.chigs.repository.ChigsFacade;
import com.googlecode.chigscommons.web.common.IconFactory;
import com.googlecode.chigscommons.web.common.ValidationStyleBehavior;
import com.googlecode.chigscommons.web.common.forms.ShinyForm;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.*;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.io.Serializable;
import java.util.List;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@AuthorizeInstantiation("ROLE_ADMIN")
public class EditOrCreateLocationGroupPanel extends Panel {
	public EditOrCreateLocationGroupPanel(String id) {
		this(id, new CompoundPropertyModel<ConfigLocationGroup>(new ConfigLocationGroup()));
	}

	public EditOrCreateLocationGroupPanel(String id, IModel<ConfigLocationGroup> model) {
		super(id);
		add(new EditOrCreateConfigLocationGroupForm("form", new CompoundPropertyModel<ConfigLocationGroup>(model.getObject())));
	}

	class EditOrCreateConfigLocationGroupForm extends ShinyForm<ConfigLocationGroup> {
		@SpringBean
		private ChigsFacade chigs;
		
		@SuppressWarnings("unchecked")
		public EditOrCreateConfigLocationGroupForm(String id, IModel<ConfigLocationGroup> model) {
			super(id, model);
			StringResourceModel lName = new StringResourceModel("name.label", EditOrCreateConfigLocationGroupForm.this, null);
			add(new Label("name.label", lName));
			add(new TextField<String>("name")
					.setRequired(true)
					.setLabel(lName)
					.add(new ValidationStyleBehavior()));

			PropertyModel<List<ConfigLocation>> cur = new PropertyModel<List<ConfigLocation>>(getModelObject(),"configLocations");
			final List<ConfigLocation> locations = chigs.findConfigLocationsByEmptyGroup();
			locations.addAll(getModelObject().getConfigLocations());
			Palette<ConfigLocation> palette = new Palette<ConfigLocation>(
					"locations", cur, new Model((Serializable) locations),
					new ChoiceRenderer<ConfigLocation>("name", "id"),
					10, false
			);
			add(palette);
			
			add(new Image("submit", IconFactory.createOkIconReference()));
			Link<Void> cancelLink = new Link<Void>("cancelButton") {
				@Override
				public void onClick() {
					returnToManagement();
				}
			};
			cancelLink.add(new Image("icon", IconFactory.createCancelIconReference()));			
			add(cancelLink);
		}

		@Override
		protected void onSubmit() {
			try {
				chigs.saveConfigLocationGroup(getModelObject());
				getSession().info(new StringResourceModel("save.success", EditOrCreateConfigLocationGroupForm.this, null).getString());
				returnToManagement();
			} catch (Exception e) {
				error(e.getMessage());
			}
		}

		private void returnToManagement() {
			final Panel p = new ManageLocationGroupsPanel(EditOrCreateLocationGroupPanel.this.getId());
			p.setOutputMarkupId(true);
			EditOrCreateLocationGroupPanel.this.getParent().addOrReplace(p);
			this.setVisible(false);
		}
	}
}
